import _thread
from bs4 import BeautifulSoup
import requests
import datetime
import re
import redis
import threading


class myThread (threading.Thread):
    def __init__(self,name, startPage,endPage):
        threading.Thread.__init__(self)
        self.startPage = startPage
        self.endPage = endPage
        self.name=name


    def run(self):
        print ("开始线程：" + self.name)
        getDoc(self.startPage,self.endPage)
        print ("退出线程：" + self.name)

def getDoc(startPage,endPage):
    num = 0
    startTime = datetime.datetime.now()
    for i in range(startPage, endPage):
        if i != 25:
            print(i)
            urlStr = "http://www.diandianys.com/f/search/lookingDoc?provinceId=330000&cityId=&gbDeptCode=&hosTypeId=&hosLevelId=&&page=" + str(
                i) + "&limit=10"
            print(urlStr)
            webstr = requests.get(urlStr).text
            Soap = BeautifulSoup(webstr, "lxml")
            docs = Soap.select(
                "body > div.doctor_main > div.doctor_main_con > div.doctor_main_con_l > ul > li > div.doctor_main_con_l_l > span > a")
            r = redis.Redis(host="192.168.1.29", port=6379, password="redisps")
            for doc in docs:
                data = {
                    "docName": doc.get_text(),
                    "docCode": re.sub(r'\D', "", doc.get("href")),
                }
                r.append(doc.get_text(), re.sub(r'\D', "", doc.get("href")))

                print(data)

                num += 1

    endTime = datetime.datetime.now()

    print(endTime - startTime)
    print("共", num, "个医生")
    print(r.get("hosinfo"))


if __name__=='__main__':
    threads = []
    startN=1
    endN=10
    begin = datetime.datetime.now()
    for i in range(1,100):
        threads.append(threading.Thread(target=getDoc, args=(startN,endN,)))
        startN=i*10+1
        endN=(i+1)*10
        print("开束始页:",startN,"结束页:",endN)

    for t in threads:
        t.start()
    end = datetime.datetime.now()

    print(end - begin)
    print("退出主线程")
